package com.example.a7822.myapplication.shopcart.common;

import android.support.annotation.Nullable;
import android.util.Log;

import com.example.a7822.myapplication.shopcart.beans.GoodBean;
import com.example.a7822.myapplication.utils.Logger;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Description :
 * Author : lauren
 * Email  : lauren.liuling@gmail.com
 * Blog   : http://www.liuling123.com
 * Date   : 15/12/19
 */
public class GoodJsonUtils {

    private final static String TAG = "GoodJsonUtils";
    private static Logger logger = Logger.getLogger(GoodJsonUtils.class);
  /*  public static List<GoodBean> readJsonGoodBeans2(String res) {
        List<GoodBean> beans = new ArrayList<GoodBean>();
        String jsonString = "{\"id\":\"2\",\"title\":\"test new2\",\"cover\":\"new3 cover\",\"content\":\"http:\\/\\/www.camera_2_record.cn\",\"pasturelId\":\"1\",\"abstract\":\"sdasdsad\"}";
        try {
            JSONObject json = new JSONObject(res);
            int status = json.getInt("status");
            if (status == 0) {
                GoodBean t;
                JSONArray jsonArray = json.getJSONArray("newsList");
                //String loginNames = "loginname list:";
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject good = (JSONObject) jsonArray.get(i);
                    t=new GoodBean();
                    t.setGoodId(good.getInt("id"));
                    t.setGoodName(good.getString("goodName"));
                    t.setGoodPic(good.getString("goodPic"));
                    t.setGoodPriceNow(good.getDouble("goodPrice"));
                    beans.add(t);
                }
            }
        } catch (Exception e) {
            LogUtils.e(TAG, "readJsonGoodBeans error", e);
        }
        return beans;
    }*/

    /**
     * 将获取到的json转换为商品列表对象
     *
     * @param res
     * @param value
     * @return
     */
    public static List<GoodBean> readJsonGoodBeans(String res, String value) {
        List<GoodBean> beans = new ArrayList<GoodBean>();
//        try {
//            JsonParser parser = new JsonParser();
//            JsonObject jsonObj = parser.parse(res).getAsJsonObject();
//            JsonElement jsonElement = jsonObj.get(value);
//            if (jsonElement == null) {
//                return null;
//            }
//            JsonArray jsonArray = jsonElement.getAsJsonArray();
//            for (int i = 1; i < jsonArray.size(); i++) {
//                JsonObject jo = jsonArray.get(i).getAsJsonObject();
//                if (jo.has("skipType") && "special".equals(jo.get("skipType").getAsString())) {
//                    continue;
//                }
//                if (jo.has("TAGS") && !jo.has("TAG")) {
//                    continue;
//                }
//
//                if (!jo.has("imgextra")) {
//                    GoodBean good = JsonUtils.deserialize(jo, GoodBean.class);
//                    beans.add(good);
//                }
//            }
//        } catch (Exception e) {
//            LogUtils.e(TAG, "readJsonGoodBeans error", e);
//        }
        return beans;
    }

    public static GoodBean readJsonGoodBean(String res, String goodCode) {
        GoodBean goodBean = null;
//        try {
//            JsonParser parser = new JsonParser();
//            JsonObject jsonObj = parser.parse(res).getAsJsonObject();
//            JsonElement jsonElement = jsonObj.get(goodCode);
//            if (jsonElement == null) {
//                return null;
//            }
//            goodBean = JsonUtils.deserialize(jsonElement.getAsJsonObject(), GoodBean.class);
//        } catch (Exception e) {
//            LogUtils.e(TAG, "readJsonGoodBeans error", e);
//        }
        return goodBean;
    }

    @Nullable
    public static GoodBean readJsonGoodBean(String res) {
        GoodBean good= new GoodBean();
        try {
            JSONObject json = new JSONObject(res);
            int status = json.getInt("status");
            if (status == 0) {
                JSONObject jsonObject = json.getJSONObject("goodInfo");
                good.setGoodId(jsonObject.getInt("id"));
                good.setGoodName(jsonObject.getString("goodName"));
                good.setGoodDesc(jsonObject.getString("goodInfo"));
                good.setGoodPic(jsonObject.getString("goodImage"));
                good.setGoodPriceNow((Math.round(jsonObject.getDouble("promotePrice")*100)/100.0));
                good.setGoodPriceBefore((Math.round(jsonObject.getDouble("goodPrice")*100)/100.0));
                good.setGoodTotalPrice();

                logger.d("扫码的商品信息为："+good.getGoodName());
                logger.d("扫码的商品图片为："+good.getGoodPic());
                logger.d("扫码的商品描述为："+good.getGoodDesc());
//                good.setGoodCode(jsonObject.getString("googCode"));
                return good;
            }
        } catch (Exception e) {
            Log.e(TAG, "readJsonGoodBeans error", e);
        }
        return null;
    }

}
